<template>
	<view>
		
		<u-navbar title="订单详情"></u-navbar>

		<!-- 收货地址 -->
		<view class="orderAddress_box">
			<view class="consigneeInfor">
				<text>{{orderDetail.consignee}} </text>
				{{orderDetail.mobile}}
			</view>
			<view class="consigneeAddress">{{orderDetail.address}}</view>
		</view>

		<!-- 商品信息 -->
		<view class="orderDetail_con">
			<view class="detailHead_con">
				<view class="detailTit">商品信息</view>
				<view class="detailStatus">{{orderDetail.order_status_str}}</view>
			</view>
			<view class="goodsList_con">
				<view class="goodsCon" v-for="(goods_list,goods_index) in orderDetail.goods_list" :key="goods_index">
					<view class="goodsPic">
						<image :src="goods_list.rec_goods_imgs"></image>
					</view>
					<view class="goodsInfor">
						<view class="goodsName">
							{{goods_list.rec_goods_name}}
						</view>
						<view class="goodsPrice">
							<text>价格：¥{{goods_list.rec_goods_price}}</text>
							<text>数量：{{goods_list.rec_goods_num}}</text>
						</view>
						<view class="goodsSpec" v-if="goods_list.spec_key_name">
							规格：{{goods_list.spec_key_name}}
						</view>
					</view>
				</view>
			</view>
			<view class="orderTotal_con">
				<view class="totalTit">共{{orderDetail.goods_list.length}}件商品</view>
				<view class="totalMoney">
					合计：
					<text>￥{{orderDetail.total_amount}}</text>
				</view>
			</view>
		</view>

		<!-- 订单信息 -->
		<view class="orderInfor_con">
			<view class="inforTit">订单信息</view>
			<view class="inforCon">
				<view class="inforLi">
					<view class="inforLabel">订单编号</view>
					<view class="inforValue">{{orderDetail.order_sn}}</view>
				</view>
				<view class="inforLi" v-if="orderDetail.shipping_name">
					<view class="inforLabel">快递公司</view>
					<view class="inforValue">{{orderDetail.shipping_name}}</view>
				</view>
				<view class="inforLi" v-if="orderDetail.shipping_code">
					<view class="inforLabel">快递单号</view>
					<view class="inforValue">{{orderDetail.shipping_code}}</view>
				</view>
				<view class="inforLi">
					<view class="inforLabel">下单时间</view>
					<view class="inforValue">{{orderDetail.add_time_str}}</view>
				</view>
				<view class="inforLi" v-if="orderDetail.shipping_time_str!='--'">
					<view class="inforLabel">发货时间</view>
					<view class="inforValue">{{orderDetail.shipping_time_str}}</view>
				</view>
				<view class="inforLi" v-if="orderDetail.confirm_time_str!='--'">
					<view class="inforLabel">收货时间</view>
					<view class="inforValue">{{orderDetail.confirm_time_str}}</view>
				</view>
				<view class="inforLi" v-if="orderDetail.pay_time_str!='--'">
					<view class="inforLabel">支付时间</view>
					<view class="inforValue">{{orderDetail.pay_time_str}}</view>
				</view>
				<view class="inforLi" v-if="orderDetail.user_note">
					<view class="inforLabel">备注信息</view>
					<view class="inforValue">{{orderDetail.user_note}}</view>
				</view>
			</view>
		</view>

		<!-- 订单操作 -->
		<view class="orderHandle_box">
			<view class="orderCancel" v-if="orderDetail.order_status==0"
				@click="orderHandle(orderDetail.order_id,'40')">
				取消订单
			</view>
			<view class="orderCancel" v-if="orderDetail.order_status>0 && orderDetail.order_status<40"
				@click="orderHandle(orderDetail.order_id,'50')">
				申请退款
			</view>
			<view class="orderPay" v-if="orderDetail.order_status==0"
				@click="payDo(orderDetail.order_id,orderDetail.total_amount)">
				立即支付
			</view>
			<view class="orderPay" v-if="orderDetail.order_status==20" @click="orderHandle(orderDetail.order_id,'30')">
				确认收货
			</view>
		</view>

	</view>
</template>

<script>
	import {
		pay
	} from '@/util/pay.js';
	export default {
		data() {
			return {
				// 订单详情
				order_id: '',
				orderDetail: {},
			}
		},
		onLoad(option) {
			uni.showLoading({
				title: '加载中'
			});
			console.log(option);
			var _this = this;
			_this.order_id = option.order_id;
		},
		onReady() {
			var _this = this;
			_this.getOrderDetail();
		},
		onShow: function() {

		},
		methods: {
			// 获取订单详情
			async getOrderDetail() {
				var _this = this;
				_this.$u.get('/api/shop.order/getDetail', {
					id: _this.order_id,
				}).then(res => {
					uni.hideLoading();
					console.log("==获取订单详情==");
					console.log(res);
					_this.orderDetail = res.data;
				}).catch(res => {
					console.log(res);
				})
			},

			// 修改订单状态
			orderHandle(order_id, value) {
				var _this = this;
				_this.$u.post('/api/shop.order/updateStatus', {
					order_id: order_id,
					value: value,
				}).then(res => {
					console.log(res);
					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					setTimeout(function() {
						_this.orderList = [];
						_this.pages = 1;
						_this.getOrderList();
					}, 2000)
				}).catch(res => {
					console.log(res);
				})
			},

			// 订单支付
			payDo(order_id, totalPrice) {
				console.log(order_id, totalPrice)
				var _this = this;
				let platform = uni.getSystemInfoSync().platform;
				// console.log(platform);
				// platform = 'android';
				if(platform=='android' || platform=='ios'){
					_this.$u.post('/api/shop.order/order_pay', {
						order_id: order_id,
					}).then(res => {
						uni.hideLoading();
						console.log("==支付结果==");
						console.log(res);
					
						pay(res, _this, function() {
							uni.showToast({
								title: res.msg,
								duration: 2000
							});
							setTimeout(function() {
								_this.pages = 1;
								_this.getOrderList();
							}, 2000)
						}, function() {
							// uni.redirectTo({
							// 	url: '/package/mall/orderList/orderList?status=all',
							// });
						});
					}).catch(res => {
						console.log(res);
					})
				}else{
					uni.showModal({
						content:'小程序暂不支持在线购买，请下载APP',
						title:'提示',
						confirmText:'去下载',
						cancelText:'暂不下载',
						success:function(e){
							if(e.confirm){
								uni.navigateTo({
									url:'/pages/agreement/agreement?id=4'
								})
							}
						}
					})
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	/* 收货地址 */
	.orderAddress_box {
		padding: 30rpx;
		background-color: #fff;
		overflow: hidden;
		margin: 20rpx 0;
	}

	.consigneeInfor {
		font-size: 26rpx;
		color: #333333;
	}

	.consigneeInfor text {
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		margin-right: 20rpx;
	}

	.consigneeAddress {
		font-size: 26rpx;
		color: #8E8E8E;
		overflow: hidden;
		margin-top: 10rpx;
	}

	/* 商品信息 */
	.orderDetail_con {
		padding: 0 30rpx;
		background: #ffffff;
		overflow: hidden;
		margin-bottom: 20rpx;
	}

	.detailHead_con {
		padding: 20rpx 0;
		font-size: 28rpx;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.detailTit {
		font-weight: bold;
	}
	
	.detailStatus{
		color: $uni-color-error;
	}

	.goodsList_con {
		overflow: hidden;
	}

	.goodsCon {
		padding: 30rpx 0;
		border-top: 1rpx solid #eee;
		border-bottom: 1rpx solid #eee;
		overflow: hidden;
	}

	.goodsPic {
		float: left;
		width: 150rpx;
		height: 150rpx;
	}

	.goodsInfor {
		float: left;
		width: 480rpx;
		margin-left: 20rpx;
	}

	.goodsName {
		font-size: 32rpx;
		color: #000;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.goodsPrice {
		font-size: 24rpx;
		color: #333;
		overflow: hidden;
		margin-top: 20rpx;
	}

	.goodsPrice text {
		margin-right: 20rpx;
	}

	.goodsSpec {
		font-size: 24rpx;
		color: #999;
		overflow: hidden;
		margin-top: 10rpx;
	}

	.orderTotal_con {
		padding: 20rpx 0;
		font-size: 28rpx;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.orderTotal_con .totalTit {
		color: #333;
	}

	.orderTotal_con .totalMoney {
		color: #333;
	}

	.orderTotal_con .totalMoney text {
		color: $uni-color-error;
	}

	/* 订单信息 */
	.orderInfor_con {
		padding: 30rpx 20rpx;
		background: #ffffff;
		border-radius: 16rpx;
		overflow: hidden;
		margin: 20rpx 0;
	}

	.inforTit {
		font-size: 28rpx;
		font-weight: bold;
		padding-bottom: 10rpx;
	}

	.inforCon {
		overflow: hidden;
	}

	.inforLi {
		overflow: hidden;
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.inforLabel {
		color: #999;
	}

	.inforValue {
		color: #000;
	}

	/* 订单操作 */
	.orderHandle_box {
		padding: 20rpx 30rpx;
		background-color: #fff;
		overflow: hidden;
		display: flex;
		justify-content: flex-end;
	}

	.orderCancel {
		padding: 0 20rpx;
		height: 56rpx;
		line-height: 56rpx;
		text-align: center;
		color: #AFAFAF;
		font-size: 26rpx;
		overflow: hidden;
	}

	.orderPay {
		padding: 0 20rpx;
		height: 56rpx;
		line-height: 56rpx;
		text-align: center;
		color: $uni-color-error;
		font-size: 26rpx;
		border: 2rpx solid $uni-color-error;
		border-radius: 34rpx;
		overflow: hidden;
		margin-left: 20rpx;
	}
</style>
